package cn.edu.nju.software.model.user;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.servlet.ServletContext;

import cn.edu.nju.software.model.DatabaseHelper;

public class Store {

	private static ServletContext svct;
	private static DatabaseHelper dbHelper;

	public Store() {
		if (dbHelper == null) {
			dbHelper = new DatabaseHelper();
		}
	}

	public static void setServletContext(ServletContext svct) {
		if (Store.svct == null) {
			Store.svct = svct;
		}
	}

	public ResultSet fetchMyTickets() {
		System.out.println("bingo");
		if (svct == null) {
			return null;
		}

		@SuppressWarnings("unchecked")
		Hashtable<Integer, Integer> store = (Hashtable<Integer, Integer>) svct
				.getAttribute("store");
		Enumeration<Integer> ids = store.keys();
		ArrayList<Integer> ts = new ArrayList<Integer>();
		while (ids.hasMoreElements()) {
			ts.add(ids.nextElement());
		}
		
		return dbHelper.fetchMyMovies(ts);
	}
	
	public ArrayList<Integer> fetchNums() {
		@SuppressWarnings("unchecked")
		Hashtable<Integer, Integer> store = (Hashtable<Integer, Integer>) svct
				.getAttribute("store");
		Enumeration<Integer> ids = store.keys();
		
		ArrayList<Integer> nums = new ArrayList<Integer>();
		
		while (ids.hasMoreElements()) {
			int value = store.get(ids.nextElement());
			nums.add(value);
		}
		return nums;
	}
}
